<template>
	<view class="statistics-container page-container">
		<!-- 时间筛选栏 -->
		<view class="time-filter-bar">
			<view class="filter-tabs">
				<view class="filter-tab" v-for="(tab, index) in timeTabs" :key="index"
					:class="{ active: currentTimeFilter === tab.value }" @click="selectTimeFilter(tab.value)">
					<text class="tab-text">{{ tab.label }}</text>
				</view>
			</view>
		</view>

		<!-- 核心数据卡片 -->
		<view class="core-stats">
			<view class="stats-card main-card">
				<view class="card-header">
					<text class="card-title">{{ getCurrentPeriodName() }}销售概览</text>
					<text class="update-time">更新时间：{{ updateTime }}</text>
				</view>
				<view class="main-stats">
					<view class="stat-item">
						<text class="stat-number">{{ statisticsData.totalSales }}</text>
						<text class="stat-label">总销量</text>
						<view class="stat-trend"
							:class="{ up: statisticsData.salesGrowth > 0, down: statisticsData.salesGrowth < 0 }">
							<text class="trend-text">{{ getTrendText(statisticsData.salesGrowth) }}</text>
						</view>
					</view>
					<view class="stat-item">
						<text class="stat-number">¥{{ statisticsData.totalAmount }}</text>
						<text class="stat-label">总销售额</text>
						<view class="stat-trend"
							:class="{ up: statisticsData.amountGrowth > 0, down: statisticsData.amountGrowth < 0 }">
							<text class="trend-text">{{ getTrendText(statisticsData.amountGrowth) }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 详细统计 -->
		<view class="detail-stats">
			<view class="stats-card">
				<view class="card-header">
					<text class="card-title">详细数据</text>
				</view>
				<view class="detail-grid">
					<view class="detail-item">
						<text class="detail-label">订单数量</text>
						<text class="detail-value">{{ statisticsData.orderCount }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">客单价</text>
						<text class="detail-value">¥{{ statisticsData.avgOrderAmount }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">商品种类</text>
						<text class="detail-value">{{ statisticsData.productTypes }}</text>
					</view>
					<view class="detail-item">
						<text class="detail-label">新客户</text>
						<text class="detail-value">{{ statisticsData.newCustomers }}</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 销售趋势图表区域 -->
		<view class="chart-section">
			<view class="stats-card">
				<view class="card-header">
					<text class="card-title">销售趋势</text>
					<view class="chart-tabs">
						<view class="chart-tab" :class="{ active: chartType === 'sales' }"
							@click="switchChart('sales')">
							<text class="chart-tab-text">销量</text>
						</view>
						<view class="chart-tab" :class="{ active: chartType === 'amount' }"
							@click="switchChart('amount')">
							<text class="chart-tab-text">销售额</text>
						</view>
					</view>
				</view>

				<!-- 简化的图表展示 -->
				<view class="simple-chart">
					<view class="chart-bars">
						<view class="chart-bar" v-for="(item, index) in currentChartData" :key="index">
							<view class="bar-container">
								<view class="bar-fill" :style="{ height: getBarHeight(item.value) + '%' }"></view>
							</view>
							<text class="bar-label">{{ item.label }}</text>
							<text class="bar-value">{{ formatChartValue(item.value) }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 商品销售排行 -->
		<view class="product-ranking">
			<view class="stats-card">
				<view class="card-header">
					<text class="card-title">商品销售排行</text>
					<text class="more-link" @click="toProductRanking">查看更多 ></text>
				</view>
				<view class="ranking-list">
					<view class="ranking-item" v-for="(item, index) in topProducts" :key="item.sku_id">
						<view class="rank-number"
							:class="{'rank-first': index === 0, 'rank-second': index === 1, 'rank-third': index === 2,'rank-normal': index > 2}">
							<text class="rank-text">{{ index + 1 }}</text>
						</view>
						<image :src="$tools.buildUrl(item.main_image)" mode="aspectFill" class="product-image"></image>
						<view class="product-info">
							<text class="product-name">{{ item.spu_name }}</text>
							<text class="product-spec">{{ item.sku_name }}</text>
						</view>
						<view class="product-stats">
							<text class="sales-count">{{ item.quantity }}件</text>
							<text class="sales-amount">¥{{ item.total_amount }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 当前时间筛选
				currentTimeFilter: 'today',
				// 图表类型
				chartType: 'sales',
				// 更新时间
				updateTime: new Date().toLocaleString(),
				// 时间筛选选项
				timeTabs: [{
						label: '今日',
						value: 'today'
					},
					{
						label: '近7天',
						value: 'week'
					},
					{
						label: '本月',
						value: 'month'
					},
					{
						label: '本季度',
						value: 'quarter'
					},
					{
						label: '本年',
						value: 'year'
					}
				],
				// 统计数据
				statisticsData: {
					totalSales: 0,
					totalAmount: 0.00,
					salesGrowth: 0,
					amountGrowth: 0,
					orderCount: 0,
					avgOrderAmount: 0,
					productTypes: 0,
					newCustomers: 0,
					chartData: {
						sales: [],
						amount: []
					}
				},
				// 热销商品
				topProducts: []
			}
		},
		computed: {
			// 当前图表数据
			currentChartData() {
				return this.statisticsData.chartData?.[this.chartType] || []
			}
		},
		onLoad() {
			this.loadSaleStatistics()
			this.loadProductSale()
		},
		methods: {
			loadSaleStatistics() {
				this.$api.agent.AgentSaleStatistics({
					dateType: this.currentTimeFilter,
				}).then(res => {
					this.statisticsData = res.data
				})
			},

			loadProductSale() {
				this.$api.agent.ProductSaleRanking({
					page: 1,
					limit: 10,
					dateType: this.currentTimeFilter,
					sortType: this.chartType
				}).then(res => {
					this.topProducts = res.data
				})
			},

			// 选择时间筛选
			selectTimeFilter(value) {
				this.currentTimeFilter = value
				this.loadSaleStatistics()
				this.loadProductSale()
			},

			// 切换图表类型
			switchChart(type) {
				this.chartType = type
				this.loadProductSale()
			},

			// 获取当前时期名称
			getCurrentPeriodName() {
				const nameMap = {
					today: '今日',
					week: '近7天',
					month: '本月',
					quarter: '本季度',
					year: '本年'
				}
				return nameMap[this.currentTimeFilter] || ''
			},

			// 获取趋势文本
			getTrendText(growth) {
				if (growth > 0) {
					return `↗ +${growth}%`
				} else if (growth < 0) {
					return `↘ ${growth}%`
				} else {
					return '→ 0%'
				}
			},

			// 获取柱状图高度
			getBarHeight(value) {
				const maxValue = Math.max(...this.currentChartData.map(item => item.value))
				return maxValue > 0 ? (value / maxValue) * 100 : 0
			},

			// 格式化图表数值
			formatChartValue(value) {
				if (this.chartType === 'amount') {
					return value >= 1000 ? `${(value / 1000).toFixed(1)}k` : value.toString()
				}
				return value.toString()
			},

			// 跳转到商品排行
			toProductRanking() {
				uni.navigateTo({
					url: `/dealer-package/product/ranking?currentTimeFilter=${this.currentTimeFilter}&sortType=${this.chartType}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.statistics-container {
		background-color: #f8f9fa;
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	}

	/* 时间筛选栏 */
	.time-filter-bar {
		background-color: white;
		border-radius: 20rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.filter-tabs {
		display: flex;
		gap: 8rpx;
	}

	.filter-tab {
		flex: 1;
		height: 60rpx;
		background-color: #f8f9fa;
		border-radius: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 2rpx solid transparent;
	}

	.filter-tab.active {
		background-color: #e8f5e8;
		border-color: #52a58a;
	}

	.tab-text {
		font-size: 26rpx;
		color: #333;
	}

	.filter-tab.active .tab-text {
		color: #52a58a;
		font-weight: 600;
	}

	/* 统计卡片 */
	.stats-card {
		background-color: white;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
	}

	.card-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.card-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.update-time {
		font-size: 24rpx;
		color: #999;
	}

	.more-link {
		font-size: 26rpx;
		color: #52a58a;
	}

	/* 核心数据 */
	.main-card {
		background: linear-gradient(135deg, #52a58a 0%, #6bb6a3 100%);
		color: white;
	}

	.main-card .card-title,
	.main-card .update-time {
		color: white;
		opacity: 0.9;
	}

	.main-stats {
		display: flex;
		gap: 40rpx;
	}

	.stat-item {
		flex: 1;
		text-align: center;
	}

	.stat-number {
		font-size: 48rpx;
		font-weight: 700;
		display: block;
		margin-bottom: 8rpx;
	}

	.stat-label {
		font-size: 26rpx;
		opacity: 0.8;
		display: block;
		margin-bottom: 10rpx;
	}

	.stat-trend {
		padding: 4rpx 12rpx;
		border-radius: 20rpx;
		background-color: rgba(255, 255, 255, 0.2);
		display: inline-block;
	}

	.trend-text {
		font-size: 22rpx;
		opacity: 0.9;
	}

	.stat-trend.up {
		background-color: rgba(76, 175, 80, 0.2);
	}

	.stat-trend.down {
		background-color: rgba(244, 67, 54, 0.2);
	}

	/* 详细统计 */
	.detail-grid {
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		gap: 30rpx;
	}

	.detail-item {
		text-align: center;
	}

	.detail-label {
		font-size: 26rpx;
		color: #666;
		display: block;
		margin-bottom: 8rpx;
	}

	.detail-value {
		font-size: 36rpx;
		font-weight: 700;
		color: #52a58a;
		display: block;
	}

	/* 图表区域 */
	.chart-tabs {
		display: flex;
		gap: 10rpx;
	}

	.chart-tab {
		padding: 8rpx 20rpx;
		background-color: #f8f9fa;
		border-radius: 20rpx;
		border: 2rpx solid transparent;
	}

	.chart-tab.active {
		background-color: #e8f5e8;
		border-color: #52a58a;
	}

	.chart-tab-text {
		font-size: 24rpx;
		color: #333;
	}

	.chart-tab.active .chart-tab-text {
		color: #52a58a;
		font-weight: 600;
	}

	/* 简化图表 */
	.simple-chart {
		margin-top: 30rpx;
	}

	.chart-bars {
		display: flex;
		align-items: flex-end;
		gap: 20rpx;
		height: 200rpx;
		padding: 20rpx 0;
	}

	.chart-bar {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		height: 100%;
	}

	.bar-container {
		flex: 1;
		width: 40rpx;
		background-color: #f0f0f0;
		border-radius: 20rpx;
		position: relative;
		display: flex;
		align-items: flex-end;
	}

	.bar-fill {
		width: 100%;
		background: linear-gradient(180deg, #52a58a 0%, #6bb6a3 100%);
		border-radius: 20rpx;
		min-height: 10rpx;
	}

	.bar-label {
		font-size: 22rpx;
		color: #666;
		margin-top: 10rpx;
	}

	.bar-value {
		font-size: 20rpx;
		color: #52a58a;
		font-weight: 600;
		margin-top: 4rpx;
	}

	/* 商品排行 */
	.ranking-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}

	.ranking-item {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.rank-number {
		width: 40rpx;
		height: 40rpx;
		border-radius: 20rpx;
		background-color: #f8f9fa;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-shrink: 0;
	}

	.rank-first {
		background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
	}

	.rank-second {
		background: linear-gradient(135deg, #c0c0c0 0%, #e8e8e8 100%);
	}

	.rank-third {
		background: linear-gradient(135deg, #cd7f32 0%, #daa520 100%);
	}

	.rank-text {
		font-size: 24rpx;
		font-weight: 600;
		color: white;
	}

	.rank-number:not(.rank-first):not(.rank-second):not(.rank-third) .rank-text {
		color: #666;
	}

	.product-image {
		width: 80rpx;
		height: 80rpx;
		border-radius: 8rpx;
		flex-shrink: 0;
	}

	.product-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		gap: 4rpx;
	}

	.product-name {
		font-size: 28rpx;
		font-weight: 600;
		color: #333;
	}

	.product-spec {
		font-size: 24rpx;
		color: #666;
	}

	.product-stats {
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		gap: 4rpx;
		flex-shrink: 0;
	}

	.sales-count {
		font-size: 26rpx;
		font-weight: 600;
		color: #52a58a;
	}

	.sales-amount {
		font-size: 24rpx;
		color: #666;
	}
</style>